<?php
/**
 * cms文章模型
 *
 *
 *
 *
 * @copyright  Copyright (c) 2007-2012 ShopNC Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.com
 * @link       http://www.shopwwi.com
 * @since      File available since Release v1.1
 */
defined('ByShopWWI') or exit('Access Invalid!');
class cms_articleModel extends Model{

    public function __construct(){
        parent::__construct('cms_article');
    }

    /**
     * 读取列表
     * @param array $condition
     *
     */
    public function getList($condition, $page=null, $order='', $field='*', $limit=''){
        $result = $this->table('cms_article')->field($field)->where($condition)->page($page)->order($order)->limit($limit)->select();
        $this->cls();
        return $result;
    }

    /**
     * 文章数量
     * @param array $condition
     * @return int
     */
    public function getCmsArticleCount($condition) {
        return $this->where($condition)->count();
    }

    /**
     * 读取列表和分类名称
     *
     */
    public function getListWithClassName($condition, $page=null, $order='', $field='*', $limit=''){
        $on = 'cms_article.article_class_id = cms_article_class.class_id';
        $result = $this->table('cms_article,cms_article_class')->field($field)->join('left')->on($on)->where($condition)->page($page)->order($order)->limit($limit)->select();
        $this->cls();
        return $result;
    }

    /**
     * 根据tag编号查询
     */
    public function getListByTagID($condition, $page=null, $order='', $field='*', $limit=''){
        $condition['relation_type'] = 1;
        $on = 'cms_article.article_id = cms_tag_relation.relation_object_id';
        $result = $this->table('cms_article,cms_tag_relation')->field($field)->join('left')->on($on)->where($condition)->page($page)->order($order)->limit($limit)->select();
        $this->cls();
        return $result;
    }

    /**
     * 读取单条记录
     * @param array $condition
     *
     */
    public function getOne($condition,$order=''){
        $result = $this->table('cms_article')->where($condition)->order($order)->find();
        return $result;
    }

    /*
     *  判断是否存在
     *  @param array $condition
     *
     */
    public function isExist($condition) {
        $result = $this->table('cms_article')->getOne($condition);
        if(empty($result)) {
            return FALSE;
        }
        else {
            return TRUE;
        }
    }

    /*
     * 增加
     * @param array $param
     * @return bool
     */
    public function save($param){
        return $this->table('cms_article')->insert($param);
    }

    /*
     * 更新
     * @param array $update
     * @param array $condition
     * @return bool
     */
    public function modify($update, $condition){
        return $this->table('cms_article')->where($condition)->update($update);
    }

    /*
     * 删除
     * @param array $condition
     * @return bool
     */
    public function drop($condition){
        $this->drop_article_image($condition);
        return $this->table('cms_article')->where($condition)->delete();
    }

    /**
     * 删除文章图片
     */
    private function drop_article_image($condition) {
        $article_list = self::getList($condition);
        if(!empty($article_list) && is_array($article_list)) {
            foreach ($article_list as $article) {
                if(!empty($article['article_image_all'])) {
                    $attachment_path = $article['article_attachment_path'];
                    $article_image_array = unserialize($article['article_image_all']);
                    if(!empty($article_image_array) && is_array($article_image_array)) {
                        foreach ($article_image_array as $key=>$value) {
                            list($base_name, $ext) = explode('.', $key);
                            $image = BASE_UPLOAD_PATH.DS.ATTACH_CMS.DS.'article'.DS.$attachment_path.DS.$key;
                            $image_list = BASE_UPLOAD_PATH.DS.ATTACH_CMS.DS.'article'.DS.$attachment_path.DS.$base_name.'_list.'.$ext;
                            $image_max = BASE_UPLOAD_PATH.DS.ATTACH_CMS.DS.'article'.DS.$attachment_path.DS.$base_name.'_max.'.$ext;
                            if(is_file($image)) {
                                @unlink($image);
                            }
                            if(is_file($image_list)) {
                                @unlink($image_list);
                            }
                            if(is_file($image_max)) {
                                @unlink($image_max);
                            }
                        }
                    }
                }

            }
        }
    }

    //得到当前选中分类
    public function article_select($para){
       $class_all= $this->table('cms_article_class')->select();
        foreach ($class_all as $_key=>$_value){

            if($para=='notice'){
                if($_value['class_name'] =='公告'){
                    return $_value['class_id'];
                }
            }

            if($para=='media'){
                if($_value['class_name'] =='企业新闻'){
                    return $_value['class_id'];
                }
            }

            if($para=='question'){
                if($_value['class_name'] =='常见问题'){
                    return $_value['class_id'];
                }
            }

            if($para=='talk_show'){
                if($_value['class_name'] =='白银讲坛'){
                    return $_value['class_id'];
                }
            }

            if($para=='consult'){
                if($_value['class_name'] =='帮助中心'){
                    return $_value['class_id'];
                }
            }

            if($para=='pay'){
                if($_value['class_name'] =='支付方式'){
                    return $_value['class_id'];
                }
            }

            if($para=='sale'){
                if($_value['class_name'] =='售后服务'){
                    return $_value['class_id'];
                }
            }

            if($para=='account'){
                if($_value['class_name'] =='客服中心'){
                    return $_value['class_id'];
                }
            }

            if($para=='profit'){
                if($_value['class_name'] =='公司介绍'){
                    return $_value['class_id'];
                }
            }

        }

}

//根据class_name,返回其类型
public function class_name($para){
        switch ($para){
            case '公告':
                return 'notice';
                break;
            case '企业新闻':
                return 'media';
                break;
            case '常见问题':
                return 'question';
                break;
            case '白银讲坛':
                return 'talk_show';
                break;
            case '帮助中心':
                return 'consult';
                break;
            case '支付方式':
                return 'pay';
                break;
            case '售后服务':
                return 'sale';
                break;
            case '客服中心':
                return 'account';
                break;
            case '公司介绍':
                return 'profit';
                break;
        }

}


//上一条或者下一条文章ID
   public function articel_list($article_id,$action,$condition){
       if($action=='next'){
           $condition['article_id']= array('gt',$article_id);
           return $this->table('cms_article')->field('article_id,article_title')->order('article_id asc')
               ->where($condition)->find();
       }
       if($action=='pre'){
           $condition['article_id']= array('lt',$article_id);
           return $this->table('cms_article')->field('article_id,article_title')->order('article_id desc')
               ->where($condition)->find();
       }
   }

   //右侧侧边栏列表
    public function bar_article($limit,$condition){
        return $this->table('cms_article')->where($condition)->order('article_modify_time desc')->field('article_id,article_title')->limit($limit)
            ->select();
    }

    //底部菜单导航
    public function nav_footer(){
        $nav=array();
        $condition= array('工作机会','合作及洽谈','联系客服','关于我们');
        $article= $this->table('cms_article')->field('article_id, article_title')->order('article_sort')->limit(1000)
            ->select();
        foreach ($article as $_value){
            if(in_array($_value['article_title'], $condition)){
                array_push($nav, $_value);
            }
        }
        return $nav;
    }

}
